#include "process_pool.h"

#define FILENAME "file"

void sigFunc(int sigNum)
{
    if(sigNum == SIGPIPE)
    {
        printf("Child :id %d  client disconneted, child crash\n",getpid());
        exit(404);
    }
}
int trans_file(int newFd)
{
    signal(SIGPIPE,sigFunc);
    //发送文件名
    train_t dataTrain;
    dataTrain.dataLen = strlen(FILENAME);//规范是要转换大小端
    strcpy(dataTrain.buf,FILENAME);
    send(newFd,&dataTrain,4+dataTrain.dataLen,0);
    //发送文件大小
    struct stat statbuf;
    stat(FILENAME,&statbuf);
    dataTrain.dataLen = sizeof(off_t);
    memcpy(dataTrain.buf,&statbuf.st_size,dataTrain.dataLen);
    send(newFd,&dataTrain,4+dataTrain.dataLen,0);
    //发送文件的内容
    int fd = open(FILENAME,O_RDONLY);
    ERROR_CHECK(fd,-1,"open");
    while((dataTrain.dataLen = read(fd,dataTrain.buf,sizeof(dataTrain.buf))))
    {
        send(newFd,&dataTrain,4+dataTrain.dataLen,0);
    }
    //发送结束标志
    dataTrain.dataLen = 0;
    send(newFd,&dataTrain,4+dataTrain.dataLen,0);
    return 0;
}

